home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / vidhandl / fnttocpp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-03  |  3.1 KB  |  122 lines

  1. /* FNTTOCPP - Converts a DOS font file (.FNT) to a C/C++ source file */
  2. /* Freeware version                                                  */
  3. /* By Marcio Afonso Arimura Fialho                                   */
  4. /* http://pessoal.iconet.com.br/jlfialho                             */
  5. /* e-mail: jlfialho@iconet.com.br or (alternate) jlfialho@yahoo.com  */
  6.  
  7. //fnttocpp converts a .FNT file to a .CPP file holding a array with
  8. //.FNT file information
  9.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13.  
  14. #include "readname.cpp"
  15.  
  16. void numtohex (unsigned num, char *s)
  17.     //converts a byte to string (in borland hex notation, plus comma and space)
  18.  {
  19.     *s='0';s++;
  20.     *s='x';s++;
  21.     *s=(num/16u)%16u;
  22.     if (*s<10u)
  23.         *s+='0';
  24.      else
  25.         *s+='7';
  26.     s++;
  27.     *s=num%16u;
  28.     if (*s<10u)
  29.         *s+='0';
  30.      else
  31.         *s+='7';
  32.     s++;
  33.     *s=','; s++;
  34.     *s=0;
  35.  }
  36.  
  37. void main (int n, char *ent[5])
  38.  {
  39.     FILE *source,*target; //source file is the archive containing target files
  40.     char sourcepath[132];
  41.     char targetpath[132];
  42.     char temp[132];
  43.     char *path;
  44.     int  c0,c1; //counters
  45.     int  cheight; //char height
  46.     int  a0; //input
  47.     if (n<5) //about the program
  48.      {
  49.         printf ("FNTTOCPP - ver 1.0\n");
  50.         printf ("usage: FNTTOCPP <source file[.FNT]> <character size> <name> <target file>\n");
  51.         printf ("By Márcio A. A. Fialho\nhttp://pessoal.iconet.com.br/jlfialho\n");
  52.         printf ("E-mail: jlfialho@iconet.com.br OR jlfialho@yahoo.com");
  53.         return;
  54.      }
  55.     readfname(sourcepath,ent[1],".FNT");
  56.     readfname(targetpath,ent[4],".CPP");
  57.  
  58.     cheight=atoi(ent[2]);
  59.     if (cheight<=0 || cheight>64)
  60.      {
  61.         printf ("ERROR: Wrong character size\n");
  62.         goto jmp1;
  63.      }
  64.  
  65.     source=fopen (sourcepath,"rb");
  66.     if(source==NULL) //source file could not be opened
  67.      {
  68.         printf ("ERROR: FILE \"%s\" DOESN'T EXIST OR COULDN'T BE READ.\n",sourcepath);
  69.         goto jmp1;
  70.      }
  71.  
  72.     target=fopen (targetpath,"rb");
  73.     if(target!=NULL) //source file could not be opened
  74.      {
  75.         printf ("ERROR: FILE \"%s\" ALREADY EXISTS.\n",targetpath);
  76.         goto jmp1;
  77.      }
  78.     fclose (target);
  79.     target=fopen (targetpath,"wb");
  80.     if(target==NULL) //source file could not be opened
  81.      {
  82.         printf ("ERROR: FILE \"%s\" COULDN'T BE OPENED FOR WRITING.\n",targetpath);
  83.       jmp1:
  84.         puts ("Type FNTTOCPP with no parameters to obtain help.\a");
  85.         fcloseall();
  86.         return;
  87.      }
  88.  
  89.     sprintf(temp,"\tchar %s[]={",ent[3]);
  90.     fputs (temp,target);
  91.  
  92.     for (c0=0;c0<256;c0++)
  93.      {
  94.         if (!(c0%16u)) //writes comments
  95.          {
  96.             sprintf(temp,"\x0d\x0a\t//Shape of characters: %.3d (%.2X)h thru \
  97. %.3d (%.2X)h\x0d\x0a",c0,(unsigned char)c0,c0+15,(unsigned char)c0+15);
  98.             fputs(temp,target);
  99.          }
  100.  
  101.         //writes a row
  102.         for (c1=0;c1<cheight;c1++)
  103.          {
  104.             if (!(c1%4u) && c1) //spacing after 4 and 16 characters
  105.              {
  106.                 fputc(' ',target);
  107.                 if (!(c1%16u))
  108.                     fputs("  ",target);
  109.              }
  110.           //converts a source file byte into C hexdecimal format and writes it.
  111.             a0=fgetc(source);
  112.             numtohex(a0, temp);
  113.             fputs (temp,target);
  114.          }
  115.  
  116.         //insert comments at the end of each row
  117.         sprintf(temp,"//%3d(%.2X)\x0d\x0a",c0,(unsigned char)c0);
  118.         fputs (temp,target);
  119.      }
  120.     fputs ("};",target);
  121.     fcloseall ();
  122.  }